package edu.hawaii.myisern.action;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;

/**
 * Provides test cases.
 * 
 * @author Jon Lao
 * @author Jared Sunouchi
 * @author Chiao-Fen Zielinski-Liu
 */
public class TestOrganizationActionBean {

  /** Get the test host. */
  private String testHost = System.getProperty("test.host");
  
  /** PMD variables */
  private String username = "username";
  private String password = "password";
  private String homeTitle = "MyISERN 1.3";
  private String addTitle = homeTitle + " - Add";
  private String editTitle = homeTitle + " - Edit";
  private String orgForm = "OrganizationForm";
  private String name = "name";
  
  /**
   * Tests the organization function for adding under normal situations.
   *
   * @throws Exception If problems occur.
   */
  @Test
  @SuppressWarnings("deprecation") //TableCell.asText() is httpunit 1.5.3, needed due to Hackystat
  public void testAddOrganizationActionBean() throws Exception {
    
    WebConversation conversation = new WebConversation();
    String url = testHost + "myisern-1-green/index.jsp";
    WebResponse response = conversation.getResponse(url);
    assertEquals("Checking index.jsp retrieval", "MyISERN 1.3 Login", response.getTitle());
    
    WebForm loginForm = response.getFormWithID("LoginForm");
    WebRequest loginRequest = loginForm.getRequest();
    response = conversation.getResponse(loginRequest);
    loginRequest = loginForm.getRequest();
    
    loginRequest.setParameter(username, username);
    loginRequest.setParameter(password, password);
    
    response = conversation.getResponse(loginRequest);
    assertEquals("Checking home.jsp retrieval", homeTitle, response.getTitle());
    
    url = testHost + "myisern-1-green/add.jsp";
    response = conversation.getResponse(url);
    assertEquals("Checking add.jsp retrieval", addTitle, response.getTitle());
    
    WebForm organizationForm = response.getFormWithID("AddOrganizationForm");
    WebRequest organizationRequest = organizationForm.getRequest();
    response = conversation.getResponse(organizationRequest);
    
    WebForm addOrganizationForm = response.getFormWithID(orgForm);
    WebRequest addOrganizationRequest = addOrganizationForm.getRequest();
    addOrganizationRequest.setParameter(name, "");
    response = conversation.getResponse(addOrganizationRequest);
    
    addOrganizationForm = response.getFormWithID(orgForm);
    addOrganizationRequest = addOrganizationForm.getRequest();
    
    addOrganizationRequest.setParameter(name, "Test Name");
    addOrganizationRequest.setParameter("type", "Test Type");
    addOrganizationRequest.setParameter("contact", "Test Contact");
    addOrganizationRequest.setParameter("affiliatedResearchers", 
        "Test Researcher 1, Test Researcher 2");
    addOrganizationRequest.setParameter("country", "Test Country");
    addOrganizationRequest.setParameter("researchKeywords", "Test Keyword 1, Test Keyword 2");
    addOrganizationRequest.setParameter("researchDescription", "Test Research Description");
    addOrganizationRequest.setParameter("homePage", "www.testing.com");
    
    response = conversation.getResponse(addOrganizationRequest);
    
    url = testHost + "myisern-1-green/edit.jsp";
    response = conversation.getResponse(url);
    assertEquals("Checking edit.jsp retrieval", editTitle, response.getTitle());
    
    organizationForm = response.getFormWithID("EditOrganizationForm");
    organizationRequest = organizationForm.getRequest();
    response = conversation.getResponse(organizationRequest);
    
    WebForm editOrganizationForm = response.getFormWithID(orgForm);
    WebRequest editOrganizationRequest = editOrganizationForm.getRequest();
    
    editOrganizationRequest.setParameter(name, "Test Name");
    
    response = conversation.getResponse(editOrganizationRequest);
    
    organizationForm = response.getFormWithID(orgForm);
    organizationRequest = organizationForm.getRequest("edit");
    response = conversation.getResponse(organizationRequest);
  }
  
  /**
   * Tests the organization function for editing under normal situations.
   *
   * @throws Exception If problems occur.
   */
  @Test
  @SuppressWarnings("deprecation") //TableCell.asText() is httpunit 1.5.3, needed due to Hackystat
  public void testEditOrganizationActionBean() throws Exception {
    WebConversation conversation = new WebConversation();
    String url = testHost + "myisern-1-green/index.jsp";
    WebResponse response = conversation.getResponse(url);
    assertEquals("Checking index.jsp retrieval", "MyISERN 1.3 Login", response.getTitle());
    
    WebForm loginForm = response.getFormWithID("LoginForm");
    WebRequest loginRequest = loginForm.getRequest();
    response = conversation.getResponse(loginRequest);
    loginRequest = loginForm.getRequest();
    
    loginRequest.setParameter(username, username);
    loginRequest.setParameter(password, password);
    
    response = conversation.getResponse(loginRequest);
    assertEquals("Checking home.jsp retrieval", homeTitle, response.getTitle());
    
    url = testHost + "myisern-1-green/edit.jsp";
    response = conversation.getResponse(url);
    assertEquals("Checking edit.jsp retrieval", editTitle, response.getTitle());
    
    WebForm organizationForm = response.getFormWithID("EditOrganizationForm");
    WebRequest organizationRequest = organizationForm.getRequest();
    response = conversation.getResponse(organizationRequest);
    
    WebForm editOrganizationForm = response.getFormWithID(orgForm);
    WebRequest editOrganizationRequest = editOrganizationForm.getRequest();
    
    editOrganizationRequest.setParameter(name, "Test Name");
    
    response = conversation.getResponse(editOrganizationRequest);
    
    editOrganizationForm = response.getFormWithID(orgForm);
    editOrganizationRequest = editOrganizationForm.getRequest("edit");
    
    editOrganizationRequest.setParameter("type", "Test Type Edited");
    
    response = conversation.getResponse(editOrganizationRequest);
    
    editOrganizationForm = response.getFormWithID(orgForm);
    editOrganizationRequest = editOrganizationForm.getRequest("remove");
    response = conversation.getResponse(editOrganizationRequest);
  }
}
